package com.example.om_export.app.report.controller;

import com.example.om_export.app.report.dao.ReportTemplateMapper;
import com.example.om_export.app.report.entity.DocumentTemplateField;
import com.example.om_export.app.report.entity.ReportTemplate;
import com.example.om_export.app.report.service.CustomerService;
import com.example.om_export.app.report.service.ReportService;
import com.example.om_export.app.report.service.ReportTemplateService;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.OutputStream;
import java.net.URLEncoder;
import java.util.List;

@Controller
@RequestMapping("/report")
public class ReportController {
    @Autowired
    private ReportService reportService;
    @Autowired
    private ReportTemplateService tempServ;
    @Autowired
    CustomerService cs;
    @Autowired
    ReportTemplateMapper rtm;
    /**
     * 跳转新增模板页面
     * @return
     */
    @RequestMapping("toAddForm")
    public String toAddForm(ModelMap modelMap){
        List<DocumentTemplateField> dtlist=reportService.getDocumentTemplateList();
        modelMap.put("dtlist", dtlist);
        return "uc_report/report-form";
    }
    /**
     * 跳转模板列表页面
     * @return
     */
    @RequestMapping("showReportList")
    public String toReportList(ModelMap modelMap){
        return "uc_report/report-list";
    }

    /**
     * 跳转报表列表页面
     * @param id
     * @param modelMap
     * @return
     */
    @RequestMapping("/showReport/{id}")
    public String showReport(@PathVariable String id,ModelMap modelMap){
        ReportTemplate template = tempServ.getReportTemplateById(id);
        List<DocumentTemplateField> fieldList = reportService.getTemplateFieldsByTemplateId(template);
        modelMap.put("template", template);
        modelMap.put("fieldList", fieldList);
        return "uc_report/report-data";
    }
    /**
     * 跳转编辑页面
     * @return
     */
    @RequestMapping("edit/{id}")
    public String edit(ModelMap modelMap,@PathVariable String id){
        ReportTemplate template = tempServ.getReportTemplateById(id);
        List<DocumentTemplateField> fieldList = reportService.getTemplateFieldsByTemplateId(template);
        List<DocumentTemplateField> dtlist=reportService.getDocumentTemplateList();
        modelMap.put("dtlist", dtlist);
        modelMap.put("template", template);
        modelMap.put("fieldList", fieldList);
        return "uc_report/report-edit";
    }

    @RequestMapping(value = "/excalDownLoad/{templateId}_{cCusCode}_{cCusName}_{comCode}_{HospitalType}_{CustTYPE}")
    public void excalDownLoad(@PathVariable String templateId, HttpServletResponse response, OutputStream out, @PathVariable String cCusCode , @PathVariable String cCusName , @PathVariable String comCode , @PathVariable String HospitalType , @PathVariable String CustTYPE){
        try {
            ReportTemplate template = rtm.selectByPrimaryKey(templateId);
            response.setContentType("application/vnd.ms-excel");
            response.addHeader("Content-Disposition", "attachment;filename="
                    + URLEncoder.encode(template.getTemplateName(), "UTF-8")+".xlsx");
            XSSFWorkbook book=cs.excalDownLoad
                    (templateId, cCusCode ,cCusName ,comCode ,HospitalType ,CustTYPE);
            book.write(out);
            out.close();
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }

}
